package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorNewsType;
import guoanmaker.operator.business.model.OperatorRefuseRecord;
import guoanmaker.operator.business.model.OperatorRefuselist;
import guoanmaker.operator.business.modelnobind.OperatorNewsTypeMove;
import guoanmaker.operator.business.repository.OperatorNewsListRepository;
import guoanmaker.operator.business.repository.OperatorNewsTypeRepository;

@Service
public class OperatorNewsTypeService implements OperatorNewsTypeServiceInterface {
	
	@PersistenceContext
	private EntityManager manager;
	
	@Autowired
	private  OperatorNewsTypeRepository operatorNewsTypeRepository;
	
	@Autowired
	private  OperatorNewsListRepository operatorNewsListRepository;
	
	@Override
	public OperatorNewsType saveOperatorNewsType(OperatorNewsType operatorNewsType) {
		String newsTypeName=operatorNewsType.getNewsTypeName();
		String jpql="select r from OperatorNewsType r where r.newsTypeName=:newsTypeName";
		Query query=manager.createQuery(jpql).setParameter("newsTypeName", newsTypeName);
		List<OperatorNewsType> list=query.getResultList();
		if(list!=null&&list.size()>0){
			return null;
		}
		return operatorNewsTypeRepository.save(operatorNewsType);
	}
	
    @Override
    public int deleteNewsTypeListById(String id) {
    	id=id==null?"":id;
		if(!id.equals("")){
			String[] str=id.split(",");
			List<String>  liststr=new ArrayList<String>();
			List<OperatorNewsType>  lists=new ArrayList<OperatorNewsType>();
			String ids="";
			OperatorNewsType oeratorNewsType=null;
			for (int i = 0; i < str.length; i++) {
				oeratorNewsType=new OperatorNewsType();
				ids=str[i];
				oeratorNewsType.setId(ids);
				lists.add(oeratorNewsType);
				liststr.add(ids);
			}
			List<OperatorNewsType>  listOpe=operatorNewsTypeRepository.findAll(liststr);
			for (int i = 0; i < listOpe.size(); i++) {
				OperatorNewsType op=listOpe.get(i);
				if(op.getStatus()>0){
					return -1;
				}
			}
			operatorNewsTypeRepository.delete(lists);
		}
		return 1;
    }
	   
    @Override
    public OperatorNewsType selectNewsTypeById(String id) {
	  return  operatorNewsTypeRepository.findOne(id);
    }

	@Override
	public Map<String, Object> getAllnewTypeByPage(Integer page, Integer size, String newsTypeName) {
		newsTypeName=newsTypeName==null?"":newsTypeName;
		Map<String,Object> map=new HashMap<String,Object>();
		String jpql="select new guoanmaker.operator.business.modelnobind.OperatorNewsTypeMove(r.id,r.newsTypeName,"
				+ "r.createDate,r.status,u.id,u.username)  "
				+ "from OperatorNewsType r left join r.operatorUser u where r.newsTypeName like :newsTypeName order by r.createDate desc";
		Query query = manager.createQuery(jpql).setParameter("newsTypeName", "%"+newsTypeName+"%");
		int count=query.getResultList().size();
		List<OperatorRefuseRecord> list=query.setMaxResults(size)
				.setFirstResult(page*size)
				.getResultList();
		map.put("count", count);
		map.put("list", list);
		return map;
	}

	@Override
	public List<OperatorNewsTypeMove> getAllNewsTypeList() {
		String jpql="select new guoanmaker.operator.business.modelnobind.OperatorNewsTypeMove(r.id,r.newsTypeName) "
				+ "from OperatorNewsType r  ";
		Query query = manager.createQuery(jpql);
		List<OperatorNewsTypeMove> list=query.getResultList();
		return list;
	}

	@Transactional
	public int updateNewsType(String newsTypeName,String id) {
		String jpql="select r from OperatorNewsType r where r.newsTypeName=:newsTypeName";
		Query query=manager.createQuery(jpql).setParameter("newsTypeName", newsTypeName);
		List<OperatorNewsType> list=query.getResultList();
		if(list!=null&&list.size()>1){
			return -1;
		}
		String jpql1="update OperatorNewsType o set o.newsTypeName=:newsTypeName where id=:id";
		Query query1=manager.createQuery(jpql1).setParameter("newsTypeName", newsTypeName).setParameter("id", id);
		return query1.executeUpdate();
	}

}
